<template>
  <div>
    <crumbs :BreadCrumb="BreadCrumb"></crumbs>
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <div class="search-box">
          <el-form-item label="体检单号">
            <el-input v-model="searchForm.orderNo" clearable></el-input>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="searchForm.customerName" clearable></el-input>
          </el-form-item>
          <el-form-item label="证件号" @change="getlist">
            <el-input v-model="searchForm.credNum" clearable @keyup.enter.native="getlist"></el-input>
          </el-form-item>
          <el-form-item label="单位名称">
            <el-input v-model="searchForm.enterpriseName" clearable></el-input>
          </el-form-item>
          <el-form-item label="二级部门">
            <el-input v-model="searchForm.twoLevelDeptName" clearable></el-input>
          </el-form-item>
          <el-form-item label="入库时间">
            <el-date-picker
              v-model="daterange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="体检状态">
            <el-select v-model="searchForm.status" clearable>
              <el-option v-for="(v,i) in orderCheckStatus" :label="v.label" :value="v.value" :key="i"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
                icon="el-icon-search"
                type="primary"
                @click="getlist"
            ></el-button>
          </el-form-item>
          <el-form-item class="staff_form_btn">
            <el-button @click="printMultiple">打印</el-button>
            <!-- <el-button >集中打印</el-button> -->
          </el-form-item>
        </div>

        <div class="options">
          <div class="options_div">
            <el-checkbox v-model="barcodePrint" style="margin-right: 80px">项目条码</el-checkbox>
            <el-checkbox v-model="orderBarcodePrint" style="margin-right: 80px">指引单条码</el-checkbox>
            <el-checkbox v-model="orderPrint" style="margin-right: 80px">指引单</el-checkbox>
          </div>
          <el-table
              ref="multipleTable"
              :data="tableData"
              tooltip-effect="dark"
              style="width: 100%"
              :row-key="record=>record.id"
              @select="selectrow"
          >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="orderNo" label="体检单号" width="200">
            </el-table-column>
            <el-table-column prop="customerName" label="姓名" width="120">
            </el-table-column>
            <el-table-column prop="sex" label="性别" width="120">
              <template slot-scope="scope">{{ scope.row.sex|formatSex }}</template>
            </el-table-column>
            <el-table-column prop="age" label="年龄" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="telephone" label="联系方式" show-overflow-tooltip></el-table-column>
            <el-table-column prop="credNum" label="证件号" show-overflow-tooltip></el-table-column>
            <el-table-column prop="twoLevelDeptName" label="二级部门">
            </el-table-column>
            <el-table-column prop="contractName" label="合同名称" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="printTime" label="打印时间" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="printCount" label="打印次数" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="printCount" label="打印次数" show-overflow-tooltip></el-table-column>

            <el-table-column label="操作" width="160" fixed="right">
              <template slot-scope="scope">
                <el-button @click="printSingle(scope.row)">单项打印</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNo"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
          >
          </el-pagination>
        </div>

      </el-form>
    </div>

    <!-- 打印指引单 -->
    <el-dialog width="70%" :visible.sync="printzhiyindan" class="top_dialog">
      <div id="printTest">
        <!-- :order给子组件传参，order是orderprint里面设置的props -->
        <orderPrint :order="printOrder"></orderPrint>
      </div>
      <span slot="footer" class="dialog-footer noprint">
				<el-button type="primary" v-print="'#printTest'" @click="doOrderPrint">打 印</el-button>
				<el-button @click="printzhiyindan = false">关 闭</el-button>
			</span>
    </el-dialog>

    <!-- 打印条码 -->
    <!-- 打印条码：table列表 -->
    <el-dialog title="打印条码" :visible.sync="printtiaoma">
      <el-table
          :row-key="record=>record.id"
          ref="multipleTable"
          :data="printItemlist"
          border
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionBarcode"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="itemName" label="项目名称" width="120">
        </el-table-column>
        <el-table-column prop="code" label="条码号">
        </el-table-column>
      </el-table>
      <div class="dfooter">
        <div class="">
          <el-button type="danger" @click="barcodePage=true">打印条码</el-button>
        </div>
      </div>
    </el-dialog>

    <!-- 打印条码：打印页面(条形码) -->
    <el-dialog width="70%" :visible.sync="barcodePage" class="top_dialog">
      <div id="barcodePrint">
        <barcodePrint :order="{barcodeList:barcodeList,peOrdersMain:printOrder}"></barcodePrint>
      </div>
      <div slot="footer" class="dialog-footer noprint">
        <el-button type="primary" v-print="'#barcodePrint'" @click="doBarcodePrint">打印</el-button>
        <el-button @click="barcodePage = false">取 消</el-button>
      </div>
    </el-dialog>

    <div style="opacity: 0; position: absolute; z-index: -1; width: 0; height: 0; overflow: hidden;">
      <!-- <barcodePrint v-if="barcodePage" :order="{barcodeList:barcodeList,peOrdersMain:printOrder}" ref="qrcode" /> -->
      <orderPrint ref="zyd" :order="printOrder"></orderPrint>
    </div>
  </div>
</template>

<script>
import crumbs from "../../views/Crumbs.vue";
import moment from 'moment'
import {formatTimeToStr} from "@src/common/utils/date";
import orderPrint from "@src/components/common/orderPint";
import barcodePrint from "@src/components/common/barcodePrint";
import VueBarcode from "vue-barcode";
import {optionsSex, optionsMarriage, groupAge, contractStatus} from "@src/common/utils/constant";

export default {
  name: "print",
  components: {
    crumbs,
    orderPrint,
    barcodePrint,
    VueBarcode,
  },
  data() {
    return {
      optionsSex:optionsSex,
      BreadCrumb: [
        {path: "", name: "检中管理"},
        {path: "/front/print", name: "集中打印"},
      ],
      orderPrint:false,
      barcodePrint:false,
      orderBarcodePrint:false,
      searchForm: {},
      pageSize: 10,
      pageNo: 1,
      total: 0,
      // 打印指引单参数
      dialogFormVisiblet:false,
      printzhiyindan:false,
      printtiaoma:false,
      barcodePage:false,
      printObj: {
        id: "printTest",
        popTitle: '打印',
        extraHead: '<meta http-equiv="Content-Language" content="zh-cn"/>'
      },
      printBarcode :{
        id: "barcodePrint",
        popTitle: '打印',
        extraHead: '<meta http-equiv="Content-Language" content="zh-cn"/>'
      },
      // 打印内容详情
      printOrder: {},
      printItemlist: [],
      barcodeList: [],
      daterange: [
        formatTimeToStr(new Date(), "yyyy-MM-dd"),
        formatTimeToStr(new Date(new Date().getTime() + 24*60*60*1000), "yyyy-MM-dd")
      ]
    }
  },
  filters: {
    formatSex: function (val) {
      var dict = {1: "男", 2: "女"}
      return dict[val]
    },
    formatType: function (val) {
      var dict = {1: "普通体检", 2: "单位体检", 3: "入职体检", 4: "职业体检", 5: "婚育体检", 6: "老年专项体检", 7: "健康体检等"}
      return dict[val]
    },
    formatStatus: function (val) {
      var dict = {1: "未登记", 2: "登记", 4: "检中", 8: "交表", 16: "初检", 32: "复检", 64: "打印"}
      return dict[val]
    },
    formatDate: function (time) {
      if (time != null && time !== "") {
        var date = new Date(time);
        return formatTimeToStr(date, "yyyy-MM-dd hh:mm");
      } else {
        return "";
      }
    },
    formatApplyTo: function (val) {
      const dict = {}
      optionsSex.forEach(v => {
        dict[v.value] = v.label
      })
      return dict[val] || val
    },
    formatPayStatus: function (val) {
      var dict = {1: "已缴费", 0:"未缴费"}
      return dict[val]
    }
  },
  created() {
    this.getlist()
  },
  methods:{
    // 列表刷新
    getlist() {
      const params = {
        ...this.searchForm
      }
      if (this.daterange && this.daterange.length) {
        params.startCreateTime = moment(this.daterange[0]).format('YYYY-MM-DD')
        params.endCreateTime = moment(this.daterange[1]).format('YYYY-MM-DD')
      }else {
        delete params.startCreateTime
        delete params.endCreateTime
      }
      this.doGetList(params)
    },
    doGetList(req){
      this.$axios.batchPrintOrderList(req)
          .then((res) => {
            if (res.code === "00000") {
              this.tableData = res.data.records;
              this.total = res.data.total;
            }
          });
    },
    rest() {
      this.searchForm = {pageNo: 1, pageSize: 10}
      this.getlist()
    },
    handleTab(tab, event) {
      this.getlist()
    },
    handleSizeChange(val) {
      this.pageSize = val
      const req = {
        ...this.searchForm,
        pageSize:this.pageSize,
        pageNo:this.pageNo,
      }
      this.doGetList(req)
    },
    handleCurrentChange(val) {
      this.pageNo = val
      const req = {
        ...this.searchForm,
        pageSize:this.pageSize,
        pageNo:this.pageNo,
      }
      this.doGetList(req)
    },
    toPrintBarcode(order) {
      this.printOrder = {
        customerName:order.customerName,
        sex:order.sex,
        age:order.age,
        company:order.company,
      }
      this.$axios.queryItemScanCodeInfo({
        orderId: order.orderId,
      }).then(res => {
        if (res.code === '00000') {
          this.printItemlist = res.data
          this.printtiaoma = true
        }
      })

    },
    selectrow(selection, row) {
      this.selectinfo = selection
    },
    toPrintZYDselect(order) {
      this.printZYD(order)
    },
    handleSelectionBarcode(val){
      this.barcodeList = val;
    },
    // 单打
    printSingle(row) {
      if (!this.orderPrint && !this.barcodePrint && !this.orderBarcodePrint) {
        this.$message.error('请先选择要打印的单据类型')
        return false
      }
      if (this.orderBarcodePrint) {
        this.printQrcode(row.orderNo, row)
      }
      if (this.orderPrint) {
        this.printZYD(row.orderId)
      }
      if (this.barcodePrint) {
        this.printProjectQrcode(row.orderId)
      }
    },
    // 多打
    printMultiple() {
      // console.log(this.selectinfo)
      if (this.selectinfo && this.selectinfo.length) {
        this.selectinfo.forEach(item => {
          this.printSingle(item)
          console.log(item)
        })
      } else {
        this.$message.error('请先选择要打印的订单')
      }
    },
    // 打印指引单条码
    printQrcode(code, info) {
      const str = `${info.customerName} ${info.age} ${info.sex == '1' ? '男' : '女'}`
      const lodop = window.LODOP
      const pname = window.localStorage.getItem('qrcodeName')
      // lodop.SET_LICENSES("","795625A3ECC8D0D4B180FE47232B2F0C","","");
      lodop.SET_LICENSES("","83A700E0C70546DE025B3B2260A1395DB69","","")
      lodop.PRINT_INIT()
      lodop.PRINT_INITA(0,0,189,113,"")
      lodop.SET_PRINTER_INDEXA('ticketPrint')
      // lodop.ADD_PRINT_BARCODE(5,35,180,80,"128B",code);
      // lodop.ADD_PRINT_TEXT(90,35,160,20, str)
      // lodop.ADD_PRINT_BARCODE("0mm","10mm","30mm","16.01mm","128B", code)
      // lodop.ADD_PRINT_TEXT(85,6,180,20, str)
      lodop.ADD_PRINT_BARCODE("0mm","0","30mm","16mm","128B", code)
      // lodop.ADD_PRINT_TEXT(85,6,180,65, str)
      lodop.ADD_PRINT_TEXT(65,6,180,20, str)
      lodop.PRINT()
    },
    // 打印项目条码
    printItemQrcode(code, name) {
      const lodop = window.LODOP
      const pname = window.localStorage.getItem('qrcodeName')
      // lodop.SET_LICENSES("","795625A3ECC8D0D4B180FE47232B2F0C","","");
      lodop.SET_LICENSES("","83A700E0C70546DE025B3B2260A1395DB69","","")
      lodop.PRINT_INIT()
      lodop.PRINT_INITA(0,0,189,113,"")
      lodop.SET_PRINTER_INDEXA('ticketPrint')
      // lodop.ADD_PRINT_BARCODE(5,35,180,80,"128B",code);
      // lodop.ADD_PRINT_TEXT(90,35,160,20, name)
      // lodop.ADD_PRINT_BARCODE("0mm","10mm","30mm","16.01mm","128B", code)
      // lodop.ADD_PRINT_TEXT(85,6,180,20, name)
      lodop.ADD_PRINT_BARCODE("0mm","10mm","30mm","16mm","128B", code)
      // lodop.ADD_PRINT_TEXT(85,6,180,65, str)
      lodop.ADD_PRINT_TEXT(65,6,180,20, str)
      lodop.PRINT()
    },
    // 打印指引单
    printZYD(ids) {
      this.$axios.getPeOrdersAllDetailsBatch({ ordersIds: [ids] }).then(res => {
        console.log('res', res)
        this.printOrder = res
        this.$refs.zyd.print()
      })
    },
    // 打印项目条码
    printProjectQrcode(ids) {
      this.$axios.queryItemScanCodeInfoBatch({ ordersIds: [ids] }).then(res => {
        if (res.data && res.data.length) {
          for(let i = 0; i < res.data.length; i++) {
            if (res.data[i].itemCodeList && res.data[i].itemCodeList.length) {
              for(let j = 0; j < res.data[i].itemCodeList.length; j++) {
                this.printItemQrcode(res.data[i].itemCodeList[j].code, res.data[i].itemCodeList[j].itemName)
              }
            }
          }
        }
      })
    }
  }
}
</script>

<style scoped>
  .options_div {
    margin-bottom: 10px;
  }
</style>
